#!/usr/bin/env python

import time
from datetime import timedelta

try:
    from HTMLParser import HTMLParser
    from urlparse import urljoin, urldefrag
except ImportError:
    from html.parser import HTMLParser
    from urllib.parse import urljoin, urldefrag

from tornado import web, httpclient, gen, ioloop, queues, options

concurrency = 10
urls = [
    'https://github.com/tornadoweb/tornado/wiki/Links',
    'http://www.baidu.com',
    'http://www.thecover.cn',
    'http://www.sogou.com',
    'http://weixin.sogou.com',
    ]

def synchronous_fetch(urls, client):
    for url in urls:
        response = client.fetch(url)
    return response

@gen.coroutine
def fetch_url(url, method='GET', headers={}, body=None):
    try:
        client = httpclient.AsyncHTTPClient()
        reqs = []
        for i in range(50):
            req = httpclient.HTTPRequest(
                url, method=method, headers=headers, body=body)
            reqs.append(req)
        # response = yield {url: client.fetch(req) for url in url}
        response = yield [client.fetch(req) for req in reqs]
        print('fetched %s' % url)

    except Exception as e:
        print('Exception: %s %s' % (e, urls))
        raise gen.Return([])

    raise gen.Return(response)


@gen.coroutine
def main():
    '''
    fetch www.baidu.com
        1. 0.933458089828  use coroutine
        2. 7.22975206375   not use
    '''
    fetching, fetched = set(), set()
    cons = []
    # async_client = httpclient.AsyncHTTPClient()
    # aync_client = httpclient.HTTPClient()

    start_async = time.time()
    # ur = [urls[2]]* 200
    headers = {
        'Cookie': 'UM_distinctid=15bf52e4c452a3-0b4cd03fbad3fc-1d2a1f03-1fa400-15bf52e4c46480; sessionid=g5jnc0arkpfwlvssjwoimhswyktzyh1y; csrftoken=SflADkaR5jPfbEzSKBP6UBMtNfW9KxSN; CNZZDATA1261003677=2064824078-1494930576-%7C1495611702'
    }
    body = 'content=%7B%22user_id%22%3A96%2C%22product_id%22%3A%2214944104558eccc73e%22%2C%22quantity%22%3A1%2C%22order_id%22%3A%22%22%7D&uri=%2Forder%2Fbuyapi&req_type=POST'
    url = 'http://120.26.168.209:10060/account/sendapi'
    res = yield fetch_url(url, method='POST', headers=headers, body=body)
    print time.time() - start_async
    import pdb; pdb.set_trace()
    # start_sync = time.time()
    # synchronous_fetch(ur, aync_client)
    # print time.time() - start_sync


class MainHandler(web.RequestHandler):

    @gen.coroutine
    def get(self):
        url = [urls[1]] * 200
        res = yield fetch_url(url)
        self.write(res[url[0]].body)
        # self.finish()


class HelloWorld(web.RequestHandler):

    def get(self):
        self.write('hello world')


if __name__ == '__main__':
    # import logging
    # logging.basicConfig()
    # options.parse_command_line()
    # app = web.Application([
    #     (r'/', MainHandler),
    #     (r'/hello', HelloWorld),
    #     ], debug=True)
    # app.listen(8889)
    io_loop = ioloop.IOLoop.current()
    io_loop.run_sync(main)
    # io_loop.start()

